<%@page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>合同价录入</title>
<%@ include file="../../common/common.jsp"%>
		
<script type="text/javascript">


function save_sign(){
	$.ajax({
		type:"post",
		url:'<%=basePath %>saveProjectInfoAction!saveProjectInfoForBatch.do',
		data:"projectInfoList="+$.toJSON(rows),
		dataType:"json",
		success:function(){
			
		}
		});
	
}

function updateActions(){
	var rowcount = $('#dg').datagrid('getRows').length;
	for(var i=0; i<rowcount; i++){
		$('#dg').datagrid('updateRow',{
			index:i,
			row:{action:''}
		});
	}
}
 
 //初始化合同信息表
$(function(){
	var s_lastIndex = -1;
	$('#dg').datagrid({
		
		url:"<%=basePath %>contractInfoAction!loadData.do?contractInfo.contractStatus=2",
		view: detailview,
		//扩展div
		detailFormatter:function(index,row){
			return '<div style="padding:2px"><table id="ddv-' + index + '"></table></div>';
		},
		 onClickRow:function(index){
				if (s_lastIndex != index){
					$('#dg').datagrid('endEdit', s_lastIndex);
					$('#dg').datagrid('beginEdit', index);
				}
				s_lastIndex = index;
		},
		
		onBeforeEdit:function(index,row){
			row.editing = true;
			updateActions();
		},
		onAfterEdit:function(index,row){
			row.editing = false;
			updateActions();
		},
		onCancelEdit:function(index,row){
			row.editing = false;
			updateActions();
		},
		onExpandRow: function(index,row){
			var lastIndex = -1;
			$('#ddv-'+index).datagrid({
				url:'<%=basePath %>loadProjectInfoAction.do?projectInfo.contractinfo_id='+row.id,
				fitColumns:true,
				singleSelect:true,
				width:'600',
				height:'auto',
				rowStyler:function(index,row){
					return "<div style='padding:2px'"+"><table id='project-" + index +"'></table></div>";
				},
				
				columns:[[
					{field:'id',title:'',hidden:true},
					{field:'projectName',title:'项目名称',width:50},
					{field:'constructionOrganization',title:'施工单位',width:50},
					{field:'pipeLength',title:'施工分队',width:50},
					
					{field:'residentCount',title:'总户数',width:30},
					{field:'buildArea',title:'总面积',width:30},	
					{field:'pitCount',title:'化粪池数',width:50},
					{field:'budgetPrice',title:'项目预算价',width:50},
					{field:'contractPrice',title:'项目合同价',width:50,editor:'numberbox'},
					{field:'action',title:'操作',width:50,formatter:formatAction}
				]],
								
				toolbar:[{
					text:'确定',
					iconCls:'icon-save',
					handler:function(){
						$('#ddv-'+index).datagrid('endEdit', lastIndex);
						var rows = $('#ddv-'+index).datagrid('getRows');
						var total =0;
						for(i=0;i<rows.length;i++){
							var tmp = rows[i].contractPrice;
							if(tmp!=''&&!isNaN(tmp)&&tmp!=null&&tmp!=undefined&&tmp!='undefined'){
								total = total+parseFloat(tmp);
							}
						}
						$.ajax({
							type:"post",
							url:'<%=basePath %>saveProjectInfoAction!saveProjectInfoForBatch.do',
							data:"projectInfoList="+$.toJSON(rows),
							dataType:"json",
							success:function(){
								
							}
							});
						
						$('#dg').datagrid('updateRow',{index:index,row:{contractPrice:total}});
							
					}
						
				}],
				
				 onClickRow:function(rowIndex){
						if (lastIndex != rowIndex){
							$('#ddv-'+index).datagrid('endEdit', lastIndex);
							$('#ddv-'+index).datagrid('beginEdit', rowIndex);
						}
						lastIndex = rowIndex;
					}
				
				
			});
			$('#dg').datagrid('fixDetailRowHeight',index);
		}
	});

});

 
 

function editrow(index){
	$('#ddv-'+index).datagrid('beginEdit', index);
}

function saverow(index){
	$('#ddv-'+index).datagrid('endEdit', index);
}
function cancelrow(index){
	$('#ddv-'+index).datagrid('cancelEdit', index);
}





function do_editrow(index){
	$('#dg').datagrid('beginEdit', index);
}

function do_saverow(index){
	$('#dg').datagrid('endEdit', index);
	
	//保存数据
	var select = $('#dg').datagrid('selectRow',index);
	var row = $('#dg').datagrid('getSelected');
	var id = row.id;
	var signPerson = row.signPerson;
	var signTime = row.signTime;
	var contractPrice = row.contractPrice;
	
	var url = '<%=basePath %>saveContractAction!signContract.do';
		
	var params = {
		id:id,
		signPerson:signPerson,
		signTime:signTime,
		contractPrice:contractPrice
		};

	jQuery.post(url,params,function(data){
		//alert(data.message);
		 if(data.message=='success'){
			 $.messager.alert('提示','保存成功');	 
		}
	
},'json');
	
	
}
function do_cancelrow(index){
	$('#dg').datagrid('cancelEdit', index);
}
 
 

</script>
</head>
<body>
	
	<input type="hidden" name="projectWorkInfoId" id="projectWorkInfoId">
	
	<table id="dg" title="合同信息" class="easyui-datagrid"
			style="width: 1130px; height: 420px" 
			toolbar="#toolbar" pagination="true" rownumbers="true"
			fitColumns="true" singleSelect="true" >
			<thead>
				<tr>
					<th field="contractNo" width="30" align="center">
						合同编号
					</th>
					<th field="contractName" width="50" align="center">
						合同名称
					</th>
					<th field="createTime" width="30" align="center">
						创建时间
					</th>
					<th field="budgetPrice" width="30" align="center">
						预算价格
					</th>
					<th field="contractPrice" width="30" align="center">
						合同价格
					</th>
					<th field="signPerson" width="30" align="center" editor="{type:'validatebox'}">
						合同签订人
					</th>
					<th field="signTime" width="30" align="center" editor="{type:'datebox'}">
						合同签订时间
					</th>
					<th field="action" width="30" align="center" formatter="formatterDo">
						操作
					</th>
				</tr>
			</thead>
	</table>
		
</body>
<script type="text/javascript">
		function formatAction(value,row,index){
			if (row.editing){
				var s = '<a href="#" onclick="saverow('+index+')">保存</a> ';
				var c = '<a href="#" onclick="cancelrow('+index+')">取消</a>';
				return s+c;
			} else {
				var e = '<a href="#" onclick="editrow('+index+')">编辑</a> ';
				return e;
			}
		}
		
		function formatterDo(value,row,index){
			//alert(row.id);
			/* if (row.editing){
				var s = '<a href="#" onclick="do_saverow('+index+')">保存</a> ';
				var c = '<a href="#" onclick="do_cancelrow('+index+')">取消</a>';
				return s+c;
			} else {
				var e = '<a href="#" onclick="do_editrow('+index+')">编辑</a> ';
				return e;
			} */
			var e = '<a href="#" onclick="do_saverow('+index+')">保存</a> ';
			return e;
		}
		
	</script>
</html>